package group2assignone.view.customer;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Airport;
import group2assignone.model.Customer;
import group2assignone.model.Flight;
import group2assignone.ucm.UCLogout;
import group2assignone.ucm.customer.UCCancelCustomer;
import group2assignone.ucm.customer.UCCancelGuest;
import group2assignone.ucm.customer.UCSearchFlights;
import group2assignone.view.OurView;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

/**
 * GUI that allows the customer to search for a flight by selecting
 * arrival and departure location and date of departure.
 * @author group2
 */
public class SearchFlightsView extends OurView {

    private Customer customer;
    private JLabel custLab;
    private JLabel departure = new JLabel("From: ");
    private JLabel dest = new JLabel("Destination: ");
    private JLabel date = new JLabel("Date: ");
    private JButton search = new JButton("Search");
    private JButton cancel = new JButton("Cancel");
    private JButton logout = new JButton("Logout");
    private String[] locStrings;
    private ArrayList<Airport> airports;
    private JComboBox deptOption;
    private JComboBox destOption;
    private JTextField datePick = new JTextField("yyyy-mm-dd");
    private GridLayout grid = new GridLayout(3, 2, 2, 1);
    private String destStr;
    private String deptStr;

    /**
     * sets the customer
     * @param cust
     */
    public SearchFlightsView(Customer cust, ArrayList<Airport> airports) {
        customer = cust;
        this.airports = airports;
    }

    /**
     * Function to add the GUI components to the frame.
     */
    @Override
    public void addComponents() {

        setTitle("Search Flights");

        locStrings = new String[airports.size()];

        for(int i = 0; i<airports.size(); i++){
            locStrings[i] = airports.get(i).getAirportLoc();
        }

        deptOption = new JComboBox(locStrings);
        destOption = new JComboBox(locStrings);

        custLab = new JLabel("Welcome " + customer.getUsername());
        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS));
        custLab.setAlignmentX(JLabel.LEFT);
        logout.setAlignmentX(JButton.RIGHT);
        northPanel.add(custLab);
        northPanel.add(Box.createHorizontalGlue());
        northPanel.add(Box.createVerticalStrut(10));
        if (!customer.getUsername().equals("Guest")) {
            northPanel.add(logout);
        }

        centerPanel.setLayout(grid);
        grid.setVgap(20);
        grid.setHgap(20);
        centerPanel.add(departure);
        centerPanel.add(deptOption);
        centerPanel.add(dest);
        centerPanel.add(destOption);
        centerPanel.add(date);
        centerPanel.add(datePick);

        southPanel.add(search);
        southPanel.add(cancel);

        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        logout.addActionListener(new UCCtoALAdapter(new UCLogout(this, customer)));
        search.addActionListener(new UCCtoALAdapter(new UCSearchFlights(customer, this)));
        if (customer.getUsername().equals("Guest")) {
            cancel.addActionListener(new UCCtoALAdapter(new UCCancelGuest(this)));
        } else {
            cancel.addActionListener(new UCCtoALAdapter(new UCCancelCustomer(customer, this)));
        }
    }

    /**
     * Saves the data from the option boxes and text box to the flight parameter
     * @param flight
     */
    public void upLoad(Flight flight) {

        int arrIndex = destOption.getSelectedIndex();
        int deptIndex = deptOption.getSelectedIndex();

        String arr = locStrings[arrIndex];
        System.out.println(arr);
        flight.setArrLoc(arr);
        String dept = locStrings[deptIndex];
        System.out.println(dept);
        flight.setDepLoc(dept);
        String dat = datePick.getText();
        System.out.println(dat);
        flight.setDate(dat);
    }
}
